using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Nihonaid.classes;
using Tiago.Data;
using System.Threading;
using Nihonaid.Properties;

namespace Nihonaid
{
    public partial class LearningForm : Form
    {
        /// <summary>
        /// Event called when the form is loaded
        /// </summary>
        private void LearningForm_Load(object sender, EventArgs e)
        {
            SetInterfaceDisabled();
            ShowNextItem();
            this.queue_watcher = new Thread(new ThreadStart(UpdateStatusQueue));
            this.queue_watcher.Start();
        }

        /// <summary>
        /// Threaded function that monitors and prints the queue sizes in the status bar
        /// </summary>
        private void UpdateStatusQueue()
        {
            while (true)
            {
                lbLearnLeft.Text = Program.queue.GetLearningItemsLeft().ToString();
                lbReviewLeft.Text = Program.queue.GetReviewItemsLeft().ToString();
                lbItemsLeft.Text = Configuration.todays_items.ToString();
                Thread.Sleep(1000);
            }
        }

        /// <summary>
        /// Loads the next reviewable item
        /// </summary>
        private void ShowNextItem()
        {
            Program.current_item = Program.queue.GetNextItem();
            if (Program.current_item != null)
            {
                txtQuestion.Text = Program.current_item.Question;
                txtAnswer.Text = "";
                txtCategory.Text = Program.current_item.CategoryName;
                txtMnemonic.Text = "";
                ShowHint(Program.current_item);
                SetInterfaceAnswer();
            }
            else
                SetInterfaceDisabled();
        }

        /// <summary>
        /// Makes the changes associated with a correct answer
        /// </summary>
        private void CorrectAnswer()
        {
            Program.queue.UpdateCurrentItem(LearningItem.AnswerType.Correct);
            ShowNextItem();
        }

        /// <summary>
        /// Makes the changes associated with a wrong answer
        /// </summary>
        private void WrongAnswer()
        {
            Program.queue.UpdateCurrentItem(LearningItem.AnswerType.Wrong);
            ShowNextItem();
        }

        /// <summary>
        /// Reveals the current answer in the interface
        /// </summary>
        private void RevealAnswer()
        {
            txtAnswer.Text = Program.current_item.Answer;
            if (Program.current_item.Mnemonic.Trim().Length > 0)
            {
                txtMnemonic.ForeColor = Color.Black;
                txtMnemonic.Text = Program.current_item.Mnemonic;
            }
            else
            {
                txtMnemonic.ForeColor = Color.Red;
                txtMnemonic.Text = "No mnemonic currently available.";
            }
            SetInterfaceGrade();
        }

        private void resetLearningDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("You are about to reset all scheduler data, which will make you start from the beginning. Are you sure you want to do this?", "Reset learning data", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (res == DialogResult.Yes)
            {
                Program.queue.ResetLearningData();
                ShowNextItem();
            }
        }

        /// <summary>
        /// Shows an hint to user to aid him/her in his learning path
        /// </summary>
        /// <param name="item">The item which hint is going to be shown</param>
        private void ShowHint(LearningItem item)
        {
            List<string> messages = new List<string>();

                if (item.Order == Configuration.milestone1)
                {
                    messages.Add("Welcome to Nihonaid! You are about to start your journey into the mastery of Japanese reading/writing building blocks.");
                    messages.Add("First you are going to begin with Hiragana. Hiragana is a syllabic script. Meaning, that you can write almost anything you hear in Japanese with Hiragana!");
                    messages.Add("Your task is to memorize the sound that goes with each Hiragana character so that you can read texts written in Hiragana. Ready? Go!");
                    Configuration.milestone1 = -1;
                }

                if (item.Order == Configuration.milestone2)
                {
                    messages.Add("You will now see two Hiragana characters you were already introduced to, but this time joined together as one Hiragana.");
                    messages.Add("Just remember the sounds of each character and join them by their order. They naturally unite into a blended sound.");
                    Configuration.milestone2 = -1;     
                }

                if (item.Order == Configuration.milestone3)
                {
                    messages.Add("Now some Hiragana characters you already know will appear again, but this time with two little lines in their upper right corner.");
                    messages.Add("These lines indicate a change in the original sound. There's no need to tell you the rules, you'll learn them naturally by repetition ^ ^.");
                    Configuration.milestone3 = -1;
                }

                if (item.Order == Configuration.milestone4)
                {
                    messages.Add("Now some Hiragana characters you already know will appear again, but this time with a little circle in their upper right corner.");
                    messages.Add("This circle indicates a change in the original sound. There's no need to tell you the rules, you'll learn them naturally by repetition ^ ^.");
                    Configuration.milestone4 = -1;
                }

                if (item.Order == Configuration.milestone5)
                {
                    messages.Add("Congratulations! You finished all the Hiragana characters! They will still keep appearing until you master them.");
                    messages.Add("Now you will be introduced to Katakana. Katakana is another syllabic script.");
                    messages.Add("You can write with Katakana anything you can with Hiragana and more!");
                    messages.Add("However, Katakana is mostly used only to write loan words.");
                    messages.Add("The same rules that applied to Hiragana, apply equally to Katakana, so you won't receive any hints throughout this phase. Ready? Go!");
                    Configuration.milestone5 = -1;
                }

                if (item.Order == Configuration.milestone6)
                {
                    messages.Add("Amazing! You finished all the Katakana characters! They will still keep appearing until you master them.");
                    messages.Add("Now you will be introduced to Kanji. Kanji is what may be called a conceptual script.");
                    messages.Add("Each Kanji character has a specific meaning. Which makes this one, the last and hardest script to learn.");
                    messages.Add("You'll be presented to about 2000 Kanji. But don't be scared, since most Kanji are made by joining other Kanji.");
                    messages.Add("By reading the mnemonics, you'll probably memorize most of them with little effort.");
                    messages.Add("This is it... you are now on your own... go for it! See you on the other side.");
                    Configuration.milestone6 = -1;
                }

                if (item.Order == Configuration.milestone7)
                {
                    messages.Add("No more mnemonics from now on, you'll have to wait for the next version... until then use simple repetition to keep learning.");
                    Configuration.milestone7 = -1;
                }

                if (item.Order == Configuration.milestone8)
                {
                    messages.Add("OUTSTANDING!!! You did it!!! You are now on about to learn the final Kanji character!");
                    messages.Add("Take a minute to relax and notice how far you've come! You should be very proud of yourself ^ ^!");
                    messages.Add("Don't stop using Nihonaid. Keep checking in daily and complete your sessions to keep your foundations strong.");
                    Configuration.milestone8 = -1;
                }

                if (messages.Count > 0)
                {
                    SetInterfaceFrozen();
                    Thread.Sleep(1000);

                    foreach (string msg in messages)
                        MessageBox.Show(msg, "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
        }
    }
}
